﻿import QtQuick 2.10
import QtQuick.Controls 2.3

Menu {
    id: menu
    property var menuList: ["移动到最前","移动至","删除"]
    signal signalDisposeMenuAction(var text,var idx);
    function createMenuItem(text,idx){
        var menuComponent = Qt.createComponent("./EMCustomMenuItem.qml");
        if(menuComponent.status == Component.Ready) {
          var createdMenu = menuComponent.createObject(menu.parent);
            createdMenu.triggered.connect(function(){
                console.log("-----------triggered----------------",text);
                signalDisposeMenuAction(text,idx);
            })
            console.log("------------text---------------",text);
            createdMenu.text = text;
            createdMenu.myIdx = idx;
            return createdMenu;
        }
        return null;
    }
    onMenuListChanged: {
        for(var i = 0;i<menuList.length;i++){
            console.log("-----------------");
            var obj = createMenuItem(menuList[i],i);
            console.debug("create obj ",obj)
            menu.addItem(obj)
        }
    }
    topPadding: 2
    bottomPadding: 2
    delegate: MenuItem {
        id: menuItem
        implicitWidth: 200
        implicitHeight: 40
        contentItem: Text {
            leftPadding: 20
            rightPadding: menuItem.arrow.width
            text: menuItem.text
            font: menuItem.font
            opacity: enabled ? 1.0 : 0.3
            color: "red"
            horizontalAlignment: Text.AlignHCenter
            verticalAlignment: Text.AlignVCenter
            elide: Text.ElideRight
        }
        background: Rectangle {
            implicitWidth: 100
            implicitHeight: 32
            opacity: enabled ? 1 : 0.3
            color: menuItem.highlighted ? "#F4f4f4" : "transparent"
        }
    }
    background: Rectangle {
        implicitWidth: 100
        implicitHeight: 33
        color: "#ffffff"
        border.color: "#c3c3c3"
        radius: 4
    }
}
